/*
 *  This sketch sends data via HTTP GET requests to data.sparkfun.com service.
 *
 *  You need to get streamId and privateKey at data.sparkfun.com and paste them
 *  below. Or just customize this script to talk to other HTTP servers.
 *
 */
#include <stdio.h>
//#include <WiFi.h>
//#include <esp_system.h>
//#include "esp_spi_flash.h"
#include <Ps3Controller.h>

int freq = 500;     // 频率
int channel = 0;    // 通道
int resolution = 8; // 分辨率

#define pwm0 16
#define pwm1 17

void pwm_init()
{
    pinMode(16, OUTPUT);
    pinMode(17, OUTPUT);
    pinMode(21, OUTPUT);
    pinMode(22, OUTPUT);
    digitalWrite(16, LOW);
    digitalWrite(17, LOW);
    digitalWrite(21, LOW);
    digitalWrite(21, LOW);
    ledcSetup(0, freq, resolution); // 设置通道0
    ledcSetup(1, freq, resolution); // 设置通道1
    ledcSetup(2, freq, resolution); // 设置通道0
    ledcSetup(3, freq, resolution); // 设置通道1
    ledcAttachPin(16, 0);           // 将通道与对应的引脚连接
    ledcAttachPin(17, 1);           // 将通道与对应的引脚连接
    ledcAttachPin(21, 2);           // 将通道与对应的引脚连接
    ledcAttachPin(22, 3);           // 将通道与对应的引脚连接
}

int player = 0;
int battery = 0;
void notify()
{
    //------ Digital cross/square/triangle/circle buttons ------
}

void onConnect()
{
    Serial.println("Connected.");
}

void setup()
{
    uint8_t bt_mac[6];
    Serial.begin(115200);
    delay(10);
    pinMode(2, OUTPUT);

    pwm_init();
    /* Print chip information */
    Serial.printf("sdk version %s, ", ESP.getSdkVersion());
    Serial.printf("%dMB flash CpuFreq %dMHz\n", ESP.getFlashChipSize() / (1024 * 1024), ESP.getCpuFreqMHz());
    uint64_t chipid = ESP.getEfuseMac();                            //The chip ID is essentially its MAC address(length: 6 bytes).
    Serial.printf("The base_mac = %04X", (uint16_t)(chipid >> 32)); //print High 2 bytes
    Serial.printf("%08X\n", (uint32_t)chipid);                      //print Low 4bytes.
    //esp_base_mac_addr_set(bt_mac);    //设置base mac
    esp_read_mac(bt_mac, ESP_MAC_BT); //读蓝牙地址
    Serial.printf("The bt_mac %2X:%2X:%2X:%2X:%2X:%2X\n", bt_mac[0], bt_mac[1], bt_mac[2], bt_mac[3], bt_mac[4], bt_mac[5]);
    Ps3.attach(notify);
    Ps3.attachOnConnect(onConnect);
    Ps3.begin();
}

int value = 0;

void loop()
{
    if (!Ps3.isConnected())
        return;

    if (Ps3.data.button.up)
    {
        Serial.println("cross");
        if (Ps3.data.button.square)
        {
            Serial.println("square");
            ledcWrite(0, 0);   // 输出PWM
            ledcWrite(1, 0);   // 输出PWM
            ledcWrite(2, 0);   // 输出PWM
            ledcWrite(3, 128); // 输出PWM
        }
        else if (Ps3.data.button.circle)
        {
            Serial.println("circle");
            ledcWrite(0, 0);   // 输出PWM
            ledcWrite(1, 128); // 输出PWM
            ledcWrite(2, 0);   // 输出PWM
            ledcWrite(3, 0);   // 输出PWM
        }
        else
        {
            ledcWrite(0, 0);   // 输出PWM
            ledcWrite(1, 128); // 输出PWM
            ledcWrite(2, 0);   // 输出PWM
            ledcWrite(3, 128); // 输出PWM
        }
    }
    else if (Ps3.data.button.down)
    {
        Serial.println("down");
        if (Ps3.data.button.square)
        {
            Serial.println("square");
            ledcWrite(0, 128); // 输出PWM
            ledcWrite(1, 0);   // 输出PWM
            ledcWrite(2, 0);   // 输出PWM
            ledcWrite(3, 0);   // 输出PWM
        }
        else if (Ps3.data.button.circle)
        {
            Serial.println("circle");
            ledcWrite(0, 0);   // 输出PWM
            ledcWrite(1, 0);   // 输出PWM
            ledcWrite(2, 128); // 输出PWM
            ledcWrite(3, 0);   // 输出PWM
        }
        else
        {
            ledcWrite(0, 128); // 输出PWM
            ledcWrite(1, 0);   // 输出PWM
            ledcWrite(2, 128); // 输出PWM
            ledcWrite(3, 0);   // 输出PWM
        }
    }
    else if (Ps3.data.button.square)
    {
        Serial.println("square");
        ledcWrite(0, 0);   // 输出PWM
        ledcWrite(1, 0);   // 输出PWM
        ledcWrite(2, 0);   // 输出PWM
        ledcWrite(3, 128); // 输出PWM
    }
    else if (Ps3.data.button.circle)
    {
        Serial.println("circle");
        ledcWrite(0, 0);   // 输出PWM
        ledcWrite(1, 128); // 输出PWM
        ledcWrite(2, 0);   // 输出PWM
        ledcWrite(3, 0);   // 输出PWM
    }
    else
    {
        ledcWrite(0, 0); // 输出PWM
        ledcWrite(1, 0); // 输出PWM
        ledcWrite(2, 0); // 输出PWM
        ledcWrite(3, 0); // 输出PWM
        /* code */
    }

    //delay(2000);
    //digitalWrite(2, !digitalRead(2));
}
